const interfaces = require("../../utils/urlconfig")

// pages/list/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    prolist:[{
      id:'ca1',
      cover:'/images/logos/Angular.png',
      title:'测试商品1',
      price:'10',
      comment:'80',
      rate:'99%'
    },{
      id:'ca2',
      cover:'/images/logos/CSS3.png',
      title:'测试商品2',
      price:'10',
      comment:'80',
      rate:'99%'
    },{
      id:'ca3',
      cover:'/images/logos/Flutter.png',
      title:'测试商品3',
      price:'10',
      comment:'80',
      rate:'99%'
    },{
      id:'ca4',
      cover:'/images/logos/HTML5.png',
      title:'测试商品4',
      price:'10',
      comment:'80',
      rate:'99%'
    },{
      id:'ca5',
      cover:'/images/logos/Node.png',
      title:'测试商品5',
      price:'10',
      comment:'80',
      rate:'99%'
    }
  ],
    prolist2:[{
      id:'ca6',
      cover:'/images/logos/CSS3.png',
      title:'测试商品',
      price:'10',
      comment:'80',
      rate:'99%'
    },
    {
      id:'ca7',
      cover:'/images/logos/Vue.png',
      title:'测试商品',
      price:'10',
      comment:'80',
      rate:'99%'
    }],
    page:0,
    size:2,
    noData:false

  },
  switchProlistDetail(e){
    // console.log(e)
    const index = e.currentTarget.dataset.index;
    // console.log(index)
    wx.navigateTo({
      url: '/pages/detail/index?id='+this.data.prolist
      [index].id
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options)
    const self = this;
  
    wx.showLoading({
      title: '等阵先啦...',
    })
    
    wx.setNavigationBarTitle({
      title:options.title,
    })

    wx.request({
      
      url: interfaces.productionsList,
      success(res){
        // self.setDate({
        //   prolist:res.data
        // })
        // console.log(self.data.prolist);
        wx.hideLoading()
      },fail(){

      }
      
    })
  },

  onPullDownRefresh:function(){
    wx.showNavigationBarLoading();
    this.setData({
      page:1,
      noData:false
    })
    wx.request({
      url: interfaces.productionsList,
      success(res){
        // self.setDate({
        //   prolist:res.data
        // })
        // console.log(self.data.prolist);
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      }
      
    })
  },

  onReachBottom:function(){
    // wx.stopPullDownRefresh();
    // wx.showNavigationBarLoading();
    const prolist = this.data.prolist;
    const prolist2 = this.data.prolist2;
    let page = this.data.page;
    this.setData({
      page:++page
    })    

    const self = this;
    wx.request({
      url: interfaces.productionsList + "/"+self.data.page,
    
      success(res){
        console.log(res.data.length);
        if(res.data.length == 5727||res.data.length==5623){
          self.setData({
            noData:true
          })
        }else{
          prolist2.forEach(element => {
            prolist.push(element);
          });
        }
        self.setData({
          prolist:prolist
        })
        console.log(res.data.length);
        wx.hideNavigationBarLoading();
      }
    })
  }
})